var cur = 0,
    len = 31,
    hasLMS = false,
    flags = [],
    suspend_flags = "",
    data_lms = ["A", "A"];

for (var i = 0; i <= len; i++) {
    flags[i] = 0;
}
flags[0] = 2;
suspend_flags = flags.join('_');


function initQuiz() {
    //quizIndex = 0;
    right = 0;
    quizShow = false;
    ans = [];
    single = [];
    key = ['A', 'B', 'C', 'D'];
    flagRight = [0, 0];
    flag = [
        [0, 0, 0, 0],
        [0, 0, 0, 0]
    ];
    $('.list span:nth-child(odd)').removeClass('quizbtns_on');
}

function goPage() {
    //console.log(cur, quizIndex, flagRight);
    initPage();
    LRshow();
    if (cur < len) {
        setFlag(cur);
        quizIndex = 0;
        $('.content').css({display: 'none'});
        $($('.content')[cur]).css({display: 'block'});
    }
    if (cur === len) {
        quizIndex = 0;
        initQuiz();
        showQuzi();
        $('.content').css({display: 'none'});
        $($('.content')[cur]).css({display: 'block'});
        $('.qdiv').css({display:'none'});
        $($('.qdiv')[0]).css({display:'block'});
    }
    if (cur > len) {
        if (flagRight[0] + flagRight[1] === 2) {
            if (quizIndex < quizNum-2) {
                initQuiz();
                quizIndex = quizIndex + 2;
                showQuzi();
            }else{
                $('.qdiv').css({display:'none'});
                $($('.qdiv')[1]).css({display:'block'});
                $('#R').css({display: 'none'});
                setFlag(len);
            }
        } else {
            alert('请你正确完成答题，方可继续通过！');
        }
    }
}

function initPage() {
    $('.csld').each(function (i, e) {
        $(e).find('img').attr('src', 'img/soff.png');
        $($(e).find('img')[0]).attr('src', 'img/son.png');
    });
    $('.sliderbox').css({display: 'none'});
    $($($('.content')[cur]).find('.sliderbox')[0]).css({display: 'block'});

}

function LRshow() {
    if (cur == 0) {
        $('#L').css({display: 'none'});
        $('#R').css({display: 'block'});
    } else {
        $('#L').css({display: 'block'});
        $('#R').css({display: 'block'});
    }
    $('.menudiv ,.helpdiv').css({display: 'none'});
    $(".simg  li").css({background: "url('img/roff.png') no-repeat"});

    if(cur >=len){
        $($(".simg  li")[len]).css({background: "url('img/ron.png') no-repeat"});
    }else{
        $($(".simg  li")[cur]).css({background: "url('img/ron.png') no-repeat"});
    }
}


window.onload = function () {
    loadPage(suspend_flags, 0);
    goPage();
    LRshow();

    $('#go').on('click', function () {
        cur = 1;
        goPage();
    });

    $('#goquiz').on('click', function () {
        cur = 31;
        goPage();
    })

    $('.LR').on('click', function () {
        var i = $('.LR').index(this);
        if (i == 0) {
            cur = cur > len ? len : cur;
            cur = cur - 1;
            $('.qdiv').css({display:'none'});
            $($('.qdiv')[0]).css({display:'block'});
            goPage();
        }
        if (i == 1) {
            cur = cur + 1;
            goPage();
        }
    });

    $(".simg  li").hover(
        function () {
            if (!platform.pc()) {
                return false;
            }
            var step = $(".simg  li").index(this);
            var leftpos = 180 + step * 19 + "px";
            var imgscr = "img/small/p_" + step + ".jpg";
            $("#small").attr('src', imgscr);
            $("#tipimg").css({left: leftpos, display: "block"});
        },
        function () {
            $("#tipimg").css({display: "none"})
        }
    );

    $("body").on('click', ".simg  li", function () {
        cur = $(".simg  li").index(this);
        if (cur === len) {
            initQuiz();
            quizIndex = 0;
            showQuzi();
        }
        goPage(cur);
    })

    $('.help').on('click', function () {
        $('.helpdiv').toggle();
    })

    $(window).unload(function () {
        myUnload();
    });
}

function init() {
    hasLMS = doLMSInitialize();
    if (hasLMS == "true") {
        var temp = loadPage(suspend_flags, 0);
        doLMSCommit();
        data_lms[0] = temp[0];
        data_lms[1] = temp[1];
        var tempAry = data_lms[0].split("_")
        for (var m = 0; m < len; m++) {
            flags[m] = tempAry[m];
        }
    }
}

function myUnload() {
    var unload_status = doLMSGetValue("cmi.core.lesson_status");
    unloadPage(unload_status);
}

function setFlag(i) {
    flags[i] = 2;
    suspend_flags = flags.join("_");
    doLMSSetValue("cmi.suspend_data", suspend_flags);
    doLMSSetValue("cmi.core.lesson_location", cur);
    if (suspend_flags.indexOf("0") == -1) {
        doLMSSetValue("cmi.core.lesson_status", "completed");
    }
    doLMSCommit();
}




var platform = {

    iPhone: function () {
        if ((navigator.userAgent).indexOf('iPhone') > -1) {
            return true;
        } else {
            return false;
        }
    },
    iPad: function () {
        if ((navigator.userAgent).indexOf('iPad') > -1) {
            return true;
        } else {
            return false;
        }
    },
    Android: function () {
        if ((navigator.userAgent).indexOf('Android') > -1) {
            return true;
        } else {
            return false;
        }
    },
    win: function () {
        if ((navigator.userAgent).indexOf('Windows') > -1) {
            return true;
        } else {
            return false;
        }
    },
    mac: function () {
        if ((navigator.userAgent).indexOf('mac') > -1) {
            return true;
        } else {
            return false;
        }
    },
    pc: function () {
        if (this.win() || this.mac()) {
            return true;
        } else {
            return false;
        }
    },
    phone: function () {
        if (!this.pc()) {
            return true;
        } else {
            return false;
        }
    }
}

/*----------显示提示内容----------*/
$("#page").on('mouseover','.circle',function(e){
    $('.tips').css({display:'block'});
    var l=parseInt( $(e.target).css('left') );
    var t=parseInt( $(e.target).css('top') );
    var data=$(e.target).attr('data');
    $('.tipcont').html(data);
    var w=$('.tips').width();
    var h=$('.tips').height();
    $('.tips').css({left:l-w/2+30+2,top:t-h+30});
});
$("#page").on('mouseout','.circle',function(){
    $('.tips').css({display:'none'});
});


$("#page").on('mouseover','.singleCir',function(e){
    var tipdiv=$(e.target).attr('data');
    $('#'+tipdiv).css({display:'block'})
});
$("#page").on('mouseout','.singleCir',function(){
    $('.singleTip').css({display:'none'});
});

$("#page").on('click','.singleCir',function(e){
    if(platform.pc()){
        return false;
    }
    var tipdiv=$(e.target).attr('data');
    if($('#'+tipdiv).css('display') == 'none'){
        $('.singleTip').css({display:'none'});
        $('#'+tipdiv).css({display:'block'});
    }else{
        $('.singleTip').css({display:'none'});
    }

});


$("#page").on('mouseover','.rectbtn',function(e){
    var t=$(e.target).attr('data');
    var smg=$(e.target).attr('smg');
    $('.tip31>img:first-child').attr('src','./img/'+smg);
    $('.tip31>img:last-child').css({top:t+'px'})
    $('.tip31').css({display:'block'});
});
$("#page").on('mouseout','.rectbtn',function(){
    $('.tip31').css({display:'none'});
});



/*----------分页切换----------*/
$("#page").on('click','.csld>img',function(e){
    var sliderbox=$(e.target).parent().parent().find('.sliderbox');
    var i=$(e.target).attr('data');
    $(sliderbox).css({display:'none'});
    $(sliderbox[i]).css({display:'block'});
    $(".csld img").attr('src','img/soff.png');
    $(this).attr('src','img/son.png');
});









